package com.coderknock.spring.boot.mybatis.mapper;

import com.coderknock.spring.boot.mybatis.model.Article;
import com.coderknock.spring.boot.mybatis.model.ArticleExample.Criteria;
import com.coderknock.spring.boot.mybatis.model.ArticleExample.Criterion;
import com.coderknock.spring.boot.mybatis.model.ArticleExample;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;

public class ArticleSqlProvider {

    public String countByExample(ArticleExample example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("article");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String deleteByExample(ArticleExample example) {
        SQL sql = new SQL();
        sql.DELETE_FROM("article");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String insertSelective(Article record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("article");
        
        if (record.getId() != null) {
            sql.VALUES("id", "#{id,jdbcType=INTEGER}");
        }
        
        if (record.getAuthor() != null) {
            sql.VALUES("author", "#{author,jdbcType=INTEGER}");
        }
        
        if (record.getPermission() != null) {
            sql.VALUES("permission", "#{permission,jdbcType=BIT}");
        }
        
        if (record.getContextPath() != null) {
            sql.VALUES("context_path", "#{contextPath,jdbcType=VARCHAR}");
        }
        
        if (record.getUrl() != null) {
            sql.VALUES("url", "#{url,jdbcType=VARCHAR}");
        }
        
        if (record.getStatus() != null) {
            sql.VALUES("status", "#{status,jdbcType=BIT}");
        }
        
        if (record.getIntro() != null) {
            sql.VALUES("intro", "#{intro,jdbcType=VARCHAR}");
        }
        
        if (record.getModifyTime() != null) {
            sql.VALUES("modify_time", "#{modifyTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getCreateTime() != null) {
            sql.VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getTitleText() != null) {
            sql.VALUES("title_text", "#{titleText,jdbcType=VARCHAR}");
        }
        
        if (record.getPv() != null) {
            sql.VALUES("pv", "#{pv,jdbcType=INTEGER}");
        }
        
        if (record.getDislikeNum() != null) {
            sql.VALUES("dislike_num", "#{dislikeNum,jdbcType=INTEGER}");
        }
        
        if (record.getLikeNum() != null) {
            sql.VALUES("like_num", "#{likeNum,jdbcType=INTEGER}");
        }
        
        if (record.getArticleType() != null) {
            sql.VALUES("article_type", "#{articleType,jdbcType=BIT}");
        }
        
        return sql.toString();
    }

    public String selectByExample(ArticleExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("author");
        sql.SELECT("permission");
        sql.SELECT("context_path");
        sql.SELECT("url");
        sql.SELECT("status");
        sql.SELECT("intro");
        sql.SELECT("modify_time");
        sql.SELECT("create_time");
        sql.SELECT("title_text");
        sql.SELECT("pv");
        sql.SELECT("dislike_num");
        sql.SELECT("like_num");
        sql.SELECT("article_type");
        sql.FROM("article");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        Article record = (Article) parameter.get("record");
        ArticleExample example = (ArticleExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("article");
        
        if (record.getId() != null) {
            sql.SET("id = #{record.id,jdbcType=INTEGER}");
        }
        
        if (record.getAuthor() != null) {
            sql.SET("author = #{record.author,jdbcType=INTEGER}");
        }
        
        if (record.getPermission() != null) {
            sql.SET("permission = #{record.permission,jdbcType=BIT}");
        }
        
        if (record.getContextPath() != null) {
            sql.SET("context_path = #{record.contextPath,jdbcType=VARCHAR}");
        }
        
        if (record.getUrl() != null) {
            sql.SET("url = #{record.url,jdbcType=VARCHAR}");
        }
        
        if (record.getStatus() != null) {
            sql.SET("status = #{record.status,jdbcType=BIT}");
        }
        
        if (record.getIntro() != null) {
            sql.SET("intro = #{record.intro,jdbcType=VARCHAR}");
        }
        
        if (record.getModifyTime() != null) {
            sql.SET("modify_time = #{record.modifyTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getTitleText() != null) {
            sql.SET("title_text = #{record.titleText,jdbcType=VARCHAR}");
        }
        
        if (record.getPv() != null) {
            sql.SET("pv = #{record.pv,jdbcType=INTEGER}");
        }
        
        if (record.getDislikeNum() != null) {
            sql.SET("dislike_num = #{record.dislikeNum,jdbcType=INTEGER}");
        }
        
        if (record.getLikeNum() != null) {
            sql.SET("like_num = #{record.likeNum,jdbcType=INTEGER}");
        }
        
        if (record.getArticleType() != null) {
            sql.SET("article_type = #{record.articleType,jdbcType=BIT}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("article");
        
        sql.SET("id = #{record.id,jdbcType=INTEGER}");
        sql.SET("author = #{record.author,jdbcType=INTEGER}");
        sql.SET("permission = #{record.permission,jdbcType=BIT}");
        sql.SET("context_path = #{record.contextPath,jdbcType=VARCHAR}");
        sql.SET("url = #{record.url,jdbcType=VARCHAR}");
        sql.SET("status = #{record.status,jdbcType=BIT}");
        sql.SET("intro = #{record.intro,jdbcType=VARCHAR}");
        sql.SET("modify_time = #{record.modifyTime,jdbcType=TIMESTAMP}");
        sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        sql.SET("title_text = #{record.titleText,jdbcType=VARCHAR}");
        sql.SET("pv = #{record.pv,jdbcType=INTEGER}");
        sql.SET("dislike_num = #{record.dislikeNum,jdbcType=INTEGER}");
        sql.SET("like_num = #{record.likeNum,jdbcType=INTEGER}");
        sql.SET("article_type = #{record.articleType,jdbcType=BIT}");
        
        ArticleExample example = (ArticleExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    protected void applyWhere(SQL sql, ArticleExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}